#ifndef ZG_EVENT_H
#define ZG_EVENT_H

#include "sys/time.h"
#include "pthread.h"
#define MAX_SIZE 2048
namespace zg_util{
    class Event{
    public:
        Event(bool autoReset= false);
        ~Event();
        int set();
        void wait();
        bool wait(long milliseconds);
        int reset();
    private:
        bool            _auto;
        volatile bool   _state;
        pthread_mutex_t _mutex;
        pthread_cond_t  _cond;
    };


    inline int Event::set()
    {
        if (pthread_mutex_lock(&_mutex))
            return -1;
        _state = true;
        if (pthread_cond_broadcast(&_cond))
        {
            pthread_mutex_unlock(&_mutex);
            return -1;
        }
        pthread_mutex_unlock(&_mutex);
        return 0;
    }


    inline int Event::reset()
    {
        if (pthread_mutex_lock(&_mutex))
            return -1;
        _state = false;
        pthread_mutex_unlock(&_mutex);
        return 0;
    }
};


#endif
